package com.atguigu.gmall.rebbit.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.rebbit.constant.RedisConst;
import com.atguigu.gmall.rebbit.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class RabbitService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送普通
    public void sendMessage(String exchange,String routingKey,Object message){
        // CorrelationData创建消息体，重新发送时的应答对象
        //GmallCorrelationData拓展CorrelationData成员，装载更多传递信息
        GmallCorrelationData correlationData = retryMessage(exchange, routingKey, message);
        //向rabbitMq发送消息
        rabbitTemplate.convertAndSend(exchange,routingKey,message,correlationData);

    }

    private GmallCorrelationData retryMessage(String exchange, String routingKey, Object message) {
        GmallCorrelationData correlationData=  new GmallCorrelationData();
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        correlationData.setId(uuid);
        correlationData.setExchange(exchange);
        correlationData.setRoutingKey(routingKey);
        correlationData.setMessage(message);
        System.out.println("uuid:"+uuid);
        //向redis中设置请求信息
        redisTemplate.opsForHash().put(RedisConst.RABBIT_PREFIX,uuid, JSONObject.toJSONString(correlationData));
        return correlationData;
    }

    //发送死信 延迟消息
    public void sendDeadLetterMapper(String exchange,String routingKey,Object message,int delayTime){
        GmallCorrelationData correlationData = retryMessage(exchange, routingKey, message);
        //设置为延迟消息
        correlationData.setIsDelay(true);
        //添加时间
        correlationData.setDelayTime(delayTime);
        System.out.println("发送消息的时间："+new Date());
        //发送延迟消息
        //参数1：交换机  参数2：路由  参数3：消息  参数4：延迟设置 参数5:设置应答需要的信息
        rabbitTemplate.convertAndSend(exchange,routingKey,message,message1 -> {
            //设置过期时间 TTL 单位毫秒 String类型
            message1.getMessageProperties().setExpiration(String.valueOf(delayTime*1000));
            return message1;
        },correlationData);

    }

    //基于插件的延迟队列
    public void sendDelayMessage(String exchange,String routingkey,Object message,int delayTime){
        GmallCorrelationData gmallCorrelationData = retryMessage(exchange, routingkey, message);
        //设置是插件延迟
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        gmallCorrelationData.setId(uuid);
        gmallCorrelationData.setIsDelay(true);
        gmallCorrelationData.setDelayTime(delayTime);
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingkey);
        gmallCorrelationData.setMessage(message);
        redisTemplate.opsForHash().put(RedisConst.RABBIT_PREFIX,uuid, JSONObject.toJSONString(gmallCorrelationData));
        System.out.println("rabbitMQ的uuid"+uuid);
        rabbitTemplate.convertAndSend(exchange,routingkey,message,message1 -> {
            System.out.println("当前发送时间"+new Date());
            message1.getMessageProperties().setDelay(delayTime*1000);
            return message1;
        },gmallCorrelationData);

    }
}
